package com.atguigu.gulimall.order.listener;

import com.alipay.api.AlipayApiException;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.service.PaymentInfoService;
import com.atguigu.gulimall.order.vo.PayAsyncVo;
import com.atguigu.gulimall.order.vo.PayVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    PaymentInfoService paymentInfoService;

    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException, AlipayApiException {
        System.out.println("收到过期的订单信息,准备关闭订单" + entity.getOrderSn());
        try {
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            //订单关闭的同时关闭支付宝支付
//            String aliPaySn =  paymentInfoService.selectAliPaySnByOrderSn(entity.getOrderSn());
//            PayAsyncVo payAsyncVo = new PayAsyncVo();
//            payAsyncVo.setTrade_no(aliPaySn);
//            alipayTemplate.close(payAsyncVo);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

//    public void closeOrderPayFunction() throws AlipayApiException, UnsupportedEncodingException {
//        if(request.getParameter("WIDout_trade_no")!=null||request.getParameter("WIDtrade_no")!=null){
//            System.out.println("装备关闭支付宝交易功能。。。。。。");
//            //商户订单号和支付宝交易号不能同时为空。 trade_no、  out_trade_no如果同时存在优先取trade_no
//            //商户订单号，和支付宝交易号二选一
//            String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//            //支付宝交易号，和商户订单号二选一
//            String trade_no = new String(request.getParameter("WIDtrade_no").getBytes("ISO-8859-1"),"UTF-8");
//            /**********************/
//            // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
//            AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
//            AlipayTradeCloseRequest alipay_request=new AlipayTradeCloseRequest();
//
//            AlipayTradeCloseModel model =new AlipayTradeCloseModel();
//            model.setOutTradeNo(out_trade_no);
//            model.setTradeNo(trade_no);
//            alipay_request.setBizModel(model);
//
//            AlipayTradeCloseResponse alipay_response=client.execute(alipay_request);
//            System.out.println(alipay_response.getBody());
//        }
//    }


}
